home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW re2c 1.1 / examples / cunroll.re < prev    next >
Encoding:
Text File  |  1995-06-01  |  5.0 KB  |  259 lines  |  [TEXT/MPS ]

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define    ADDEQ    257
  6. #define    ANDAND    258
  7. #define    ANDEQ    259
  8. #define    ARRAY    260
  9. #define    ASM    261
  10. #define    AUTO    262
  11. #define    BREAK    263
  12. #define    CASE    264
  13. #define    CHAR    265
  14. #define    CONST    266
  15. #define    CONTINUE    267
  16. #define    DECR    268
  17. #define    DEFAULT    269
  18. #define    DEREF    270
  19. #define    DIVEQ    271
  20. #define    DO    272
  21. #define    DOUBLE    273
  22. #define    ELLIPSIS    274
  23. #define    ELSE    275
  24. #define    ENUM    276
  25. #define    EQL    277
  26. #define    EXTERN    278
  27. #define    FCON    279
  28. #define    FLOAT    280
  29. #define    FOR    281
  30. #define    FUNCTION    282
  31. #define    GEQ    283
  32. #define    GOTO    284
  33. #define    ICON    285
  34. #define    ID    286
  35. #define    IF    287
  36. #define    INCR    288
  37. #define    INT    289
  38. #define    LEQ    290
  39. #define    LONG    291
  40. #define    LSHIFT    292
  41. #define    LSHIFTEQ    293
  42. #define    MODEQ    294
  43. #define    MULEQ    295
  44. #define    NEQ    296
  45. #define    OREQ    297
  46. #define    OROR    298
  47. #define    POINTER    299
  48. #define    REGISTER    300
  49. #define    RETURN    301
  50. #define    RSHIFT    302
  51. #define    RSHIFTEQ    303
  52. #define    SCON    304
  53. #define    SHORT    305
  54. #define    SIGNED    306
  55. #define    SIZEOF    307
  56. #define    STATIC    308
  57. #define    STRUCT    309
  58. #define    SUBEQ    310
  59. #define    SWITCH    311
  60. #define    TYPEDEF    312
  61. #define    UNION    313
  62. #define    UNSIGNED    314
  63. #define    VOID    315
  64. #define    VOLATILE    316
  65. #define    WHILE    317
  66. #define    XOREQ    318
  67. #define    EOI    319
  68.  
  69. typedef unsigned int uint;
  70. typedef unsigned char uchar;
  71.  
  72. #define    BSIZE    8192
  73.  
  74. #define    YYCTYPE        uchar
  75. #define    YYCURSOR    cursor
  76. #define    YYLIMIT        s->lim
  77. #define    YYMARKER    s->ptr
  78. #define    YYFILL(n)    {cursor = fill(s, cursor);}
  79.  
  80. #define    RET(i)    {s->cur = cursor; return i;}
  81.  
  82. typedef struct Scanner {
  83.     int            fd;
  84.     uchar        *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof;
  85.     uint        line;
  86. } Scanner;
  87.  
  88. uchar *fill(Scanner *s, uchar *cursor){
  89.     if(!s->eof){
  90.     uint cnt = s->tok - s->bot;
  91.     if(cnt){
  92.         memcpy(s->bot, s->tok, s->lim - s->tok);
  93.         s->tok = s->bot;
  94.         s->ptr -= cnt;
  95.         cursor -= cnt;
  96.         s->pos -= cnt;
  97.         s->lim -= cnt;
  98.     }
  99.     if((s->top - s->lim) < BSIZE){
  100.         uchar *buf = (uchar*) malloc(((s->lim - s->bot) + BSIZE)*sizeof(uchar));
  101.         memcpy(buf, s->tok, s->lim - s->tok);
  102.         s->tok = buf;
  103.         s->ptr = &buf[s->ptr - s->bot];
  104.         cursor = &buf[cursor - s->bot];
  105.         s->pos = &buf[s->pos - s->bot];
  106.         s->lim = &buf[s->lim - s->bot];
  107.         s->top = &s->lim[BSIZE];
  108.         free(s->bot);
  109.         s->bot = buf;
  110.     }
  111.     if((cnt = read(s->fd, (char*) s->lim, BSIZE)) != BSIZE){
  112.         s->eof = &s->lim[cnt]; *(s->eof)++ = '\n';
  113.     }
  114.     s->lim += cnt;
  115.     }
  116.     return cursor;
  117. }
  118.  
  119. int scan(Scanner *s){
  120.     uchar *cursor = s->cur;
  121. std:
  122.     s->tok = cursor;
  123. /*!re2c
  124. any    = [\000-\377];
  125. O    = [0-7];
  126. D    = [0-9];
  127. L    = [a-zA-Z_];
  128. I    = L|D;
  129. H    = [a-fA-F0-9];
  130. E    = [Ee] [+-]? D+;
  131. FS    = [fFlL];
  132. IS    = [uUlL]*;
  133. ESC    = [\\] ([abfnrtv?'"\\] | "x" H+ | O+);
  134. X    = any\[*/];
  135. */
  136.  
  137. /*!re2c
  138.     "/*"            { goto comment; }
  139.     
  140.     
  141.     L            { RET(ID); }
  142.     L I            { RET(ID); }
  143.     L I I            { RET(ID); }
  144.     L I I I            { RET(ID); }
  145.     L I I I I        { RET(ID); }
  146.     L I I I I I        { RET(ID); }
  147.     L I I I I I I        { RET(ID); }
  148.     L I I I I I I I        { RET(ID); }
  149.     L I*            { RET(ID); }
  150.     
  151.     ("0" [xX] H+ IS?) | ("0" D+ IS?) | (D+ IS?) |
  152.     (['] (ESC|any\[\n\\'])* ['])
  153.                 { RET(ICON); }
  154.     
  155.     (D+ E FS?) | (D* "." D+ E? FS?) | (D+ "." D* E? FS?)
  156.                 { RET(FCON); }
  157.     
  158.     (["] (ESC|any\[\n\\"])* ["])
  159.                 { RET(SCON); }
  160.     
  161.     "..."                   { RET(ELLIPSIS); }
  162.     ">>="            { RET(RSHIFTEQ); }
  163.     "<<="            { RET(LSHIFTEQ); }
  164.     "+="            { RET(ADDEQ); }
  165.     "-="            { RET(SUBEQ); }
  166.     "*="            { RET(MULEQ); }
  167.     "/="            { RET(DIVEQ); }
  168.     "%="            { RET(MODEQ); }
  169.     "&="            { RET(ANDEQ); }
  170.     "^="            { RET(XOREQ); }
  171.     "|="            { RET(OREQ); }
  172.     ">>"            { RET(RSHIFT); }
  173.     "<<"            { RET(LSHIFT); }
  174.     "++"            { RET(INCR); }
  175.     "--"            { RET(DECR); }
  176.     "->"            { RET(DEREF); }
  177.     "&&"            { RET(ANDAND); }
  178.     "||"            { RET(OROR); }
  179.     "<="            { RET(LEQ); }
  180.     ">="            { RET(GEQ); }
  181.     "=="            { RET(EQL); }
  182.     "!="            { RET(NEQ); }
  183.     ";"            { RET(';'); }
  184.     "{"            { RET('{'); }
  185.     "}"            { RET('}'); }
  186.     ","            { RET(','); }
  187.     ":"            { RET(':'); }
  188.     "="            { RET('='); }
  189.     "("            { RET('('); }
  190.     ")"            { RET(')'); }
  191.     "["            { RET('['); }
  192.     "]"            { RET(']'); }
  193.     "."            { RET('.'); }
  194.     "&"            { RET('&'); }
  195.     "!"            { RET('!'); }
  196.     "~"            { RET('~'); }
  197.     "-"            { RET('-'); }
  198.     "+"            { RET('+'); }
  199.     "*"            { RET('*'); }
  200.     "/"            { RET('/'); }
  201.     "%"            { RET('%'); }
  202.     "<"            { RET('<'); }
  203.     ">"            { RET('>'); }
  204.     "^"            { RET('^'); }
  205.     "|"            { RET('|'); }
  206.     "?"            { RET('?'); }
  207.  
  208.  
  209.     [ \t\v\f]+        { goto std; }
  210.  
  211.     "\n"
  212.         {
  213.         if(cursor == s->eof) RET(EOI);
  214.         s->pos = cursor; s->line++;
  215.         goto std;
  216.         }
  217.  
  218.     any
  219.         {
  220.         printf("unexpected character: %c\n", *s->tok);
  221.         goto std;
  222.         }
  223. */
  224.  
  225. comment:
  226. /*!re2c
  227.     "*/"            { goto std; }
  228.     "\n"
  229.         {
  230.         if(cursor == s->eof) RET(EOI);
  231.         s->tok = s->pos = cursor; s->line++;
  232.         goto comment;
  233.         }
  234.         X            { goto comment; }
  235.         X X            { goto comment; }
  236.         X X X            { goto comment; }
  237.         X X X X            { goto comment; }
  238.         X X X X    X        { goto comment; }
  239.         X X X X    X X        { goto comment; }
  240.         X X X X    X X X        { goto comment; }
  241.         X X X X    X X X X        { goto comment; }
  242.         any            { goto comment; }
  243. */
  244. }
  245.  
  246. main(){
  247.     Scanner in;
  248.     int t;
  249.     memset((char*) &in, 0, sizeof(in));
  250.     in.fd = 0;
  251.     while((t = scan(&in)) != EOI){
  252. /*
  253.     printf("%d\t%.*s\n", t, in.cur - in.tok, in.tok);
  254.     printf("%d\n", t);
  255. */
  256.     }
  257.     close(in.fd);
  258. }
  259.